package fr.efrei.international.java.assignment.two;

import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class BrowseTable extends JFrame {

	static DataLoader dl = new DataLoader();
	Object[] columTitle = { "Level", "ID", "Hanzi", "PinYin", "Translations" };
	Object[] columTitle1 = { "Date", "Status", "Level", "Hanzi", "PinYin",
			"Translations" };
	Object[][] tableData = null;
	JTable table = null;
	DataLoader wr = new DataLoader();
	Record record = null;

	public BrowseTable() {
		super("Chinese Learning System");
		setSize(1000, 700);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		DefaultTableModel model = new DefaultTableModel(tableData, columTitle) {

		};
		tableInit(model);
		// show the frame at the center of the screen
		setLocationRelativeTo(null);
		setVisible(true);
		fillTable(dl.datalist);
	}

	public BrowseTable(Record r) {
		super("Chinese Learning System");
		this.record = r;

		setSize(1000, 500);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		DefaultTableModel model = new DefaultTableModel(tableData, columTitle1) {

		};
		tableInit1(model);
		// show the frame at the center of the screen
		setLocationRelativeTo(null);
		setVisible(true);
		fillTable(dl.datalist, r.readRecord());
	}


	private void tableInit1(DefaultTableModel model) {
		table = new JTable(model);
		// set width for every column
		TableColumn firsetColumn = table.getColumnModel().getColumn(0);
		firsetColumn.setPreferredWidth(80);
		firsetColumn.setMaxWidth(80);
		firsetColumn.setMinWidth(80);
		TableColumn secondColumn = table.getColumnModel().getColumn(1);
		secondColumn.setPreferredWidth(90);
		secondColumn.setMaxWidth(90);
		secondColumn.setMinWidth(90);
		TableColumn thirdColumn = table.getColumnModel().getColumn(2);
		thirdColumn.setPreferredWidth(50);
		thirdColumn.setMaxWidth(50);
		thirdColumn.setMinWidth(50);
		TableColumn fourthColumn = table.getColumnModel().getColumn(3);
		fourthColumn.setPreferredWidth(70);
		fourthColumn.setMaxWidth(70);
		fourthColumn.setMinWidth(70);
		TableColumn fifthColumn = table.getColumnModel().getColumn(4);
		fifthColumn.setPreferredWidth(120);
		fifthColumn.setMaxWidth(120);
		fifthColumn.setMinWidth(120);
		add(new JScrollPane(table), BorderLayout.CENTER);
	}


	private void tableInit(DefaultTableModel model) {
		table = new JTable(model);
		// set width for every column\
		TableColumn firsetColumn = table.getColumnModel().getColumn(0);
		firsetColumn.setPreferredWidth(70);
		firsetColumn.setMaxWidth(70);
		firsetColumn.setMinWidth(70);
		TableColumn secondColumn = table.getColumnModel().getColumn(1);
		secondColumn.setPreferredWidth(90);
		secondColumn.setMaxWidth(90);
		secondColumn.setMinWidth(90);
		TableColumn thirdColumn = table.getColumnModel().getColumn(2);
		thirdColumn.setPreferredWidth(100);
		thirdColumn.setMaxWidth(100);
		thirdColumn.setMinWidth(100);
		TableColumn fourthColumn = table.getColumnModel().getColumn(3);
		fourthColumn.setPreferredWidth(120);
		fourthColumn.setMaxWidth(120);
		fourthColumn.setMinWidth(120);
		add(new JScrollPane(table), BorderLayout.CENTER);
	}

	public void fillTable(ArrayList<CharacterData> data) {
		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		// remove existing row
		tableModel.setRowCount(0);

		for (int i = 0; i < data.size(); i++) {
			String[] arr = new String[5];
			arr[0] = String.valueOf(data.get(i).getLevel());
			arr[1] = String.valueOf(data.get(i).getId());
			arr[2] = data.get(i).getHanzi();
			arr[3] = data.get(i).getPinyin();
			arr[4] = data.get(i).getTranslations();

			tableModel.addRow(arr);
		}

		// update table
		table.invalidate();
	}

	public void fillTable(ArrayList<CharacterData> data,
			ArrayList<UserRecord> urList) {
		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		// remove existing row
		tableModel.setRowCount(0);

		for (int i = 0; i < urList.size(); i++) {
			String[] arr = new String[6];
			int m = urList.get(i).getId();
			arr[0] = urList.get(i).getData();
			if (Boolean.valueOf(urList.get(i).hasKnown))
				arr[1] = "I Know";
			else
				arr[1] = "I don't Know";
			arr[2] = String.valueOf(data.get(m - 1).getLevel());
			arr[3] = data.get(m - 1).getHanzi();
			arr[4] = data.get(m - 1).getPinyin();
			arr[5] = data.get(m - 1).getTranslations();

			tableModel.addRow(arr);
		}

		// update table
		table.invalidate();
	}
}
